home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / GETOPT.C < prev    next >
C/C++ Source or Header  |  1992-11-27  |  3KB  |  102 lines

  1. /*
  2. ** @(#)getopt.c   2.2 (smail) 1/26/87
  3.  
  4.    01 Oct 89   Added function prototype for getopt()              ahd
  5.    22 Dec 91   Use Mitch Mitchell's fixes for - option            ahd
  6. */
  7.  
  8. /*
  9.  * Here's something you've all been waiting for:  the AT&T public domain
  10.  * source for getopt(3).  It is the code which was given out at the 1985
  11.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  12.  * directly from AT&T.  The people there assure me that it is indeed
  13.  * in the public domain.
  14.  *
  15.  * There is no manual page.  That is because the one they gave out at
  16.  * UNIFORUM was slightly different from the current System V Release 2
  17.  * manual page.  The difference apparently involved a note about the
  18.  * famous rules 5 and 6, recommending using white space between an option
  19.  * and its first argument, and not grouping options that have arguments.
  20.  * Getopt itself is currently lenient about both of these things White
  21.  * space is allowed, but not mandatory, and the last option in a group can
  22.  * have an argument.  That particular version of the man page evidently
  23.  * has no official existence, and my source at AT&T did not send a copy.
  24.  * The current SVR2 man page reflects the actual behavor of this getopt.
  25.  * However, I am not about to post a copy of anything licensed by AT&T.
  26.  */
  27.  
  28. #ifdef BSD
  29. #include <strings.h>
  30. #else
  31. #include <string.h>
  32. #define  index strchr
  33. #endif
  34. #include <stdio.h>
  35.  
  36. #define ERR(s,c) fprintf(stderr,"%s%s%c\n", argv[0],s,c)    /* ahd   */
  37.  
  38. /*--------------------------------------------------------------------*/
  39. /*                    UUPC/extended include files                     */
  40. /*--------------------------------------------------------------------*/
  41.  
  42. #include "lib.h"
  43. #include "getopt.h"
  44.  
  45. int   optind = 1;
  46. int   optopt;
  47. char  *optarg;
  48.  
  49. int getopt(int argc, char **argv, char *opts)
  50. {
  51.    static int sp = 1;
  52.    register int c;
  53.    register char *cp;
  54.  
  55.    if (optind < argc && argv[optind][0] == '-' && argv[optind][1] == '\0')
  56.        if((cp=index(opts, '-')) != NULL) {
  57.            optind++;
  58.            return('-');
  59.        } else {
  60.            optind++;
  61.            return('?');
  62.        }
  63.  
  64.    if(sp == 1)
  65.       if(optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
  66.          return(EOF);
  67.       else if(strcmp(argv[optind], "--") == 0) {
  68.          optind++;
  69.          return(EOF);
  70.       }
  71.  
  72.    optopt = c = argv[optind][sp];
  73.  
  74.    if(c == ':' || (cp=index(opts, c)) == NULL) {
  75.       ERR(": illegal option -- ", c);
  76.       if(argv[optind][++sp] == '\0') {
  77.          optind++;
  78.          sp = 1;
  79.       }
  80.       return('?');
  81.    }
  82.  
  83.    if(*++cp == ':') {
  84.       if(argv[optind][sp+1] != '\0')
  85.          optarg = &argv[optind++][sp+1];
  86.       else if(++optind >= argc) {
  87.          ERR(": option requires an argument -- ", c);
  88.          sp = 1;
  89.          return('?');
  90.       } else
  91.          optarg = argv[optind++];
  92.       sp = 1;
  93.    } else {
  94.       if(argv[optind][++sp] == '\0') {
  95.          sp = 1;
  96.          optind++;
  97.       }
  98.       optarg = NULL;
  99.    }
  100.    return(c);
  101. } /* getopt */
  102.